package com.example.administrator.xtu_community.Adapter;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.xtu_community.R;

import com.example.administrator.xtu_community.simpleClass.News;

/**
 * @author 陈巧
 */
public class NewsRecyclerViewAdapter extends RecyclerView.Adapter<NewsRecyclerViewAdapter.ViewHolder> {
    private List<News> mNewsList;

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView newsDate;
        TextView newsTitle;
        TextView newsContext;
        ImageView newsImage;
        TextView newsName;

        public ViewHolder(View view) {
            super(view);

            newsDate = view.findViewById(R.id.news_date);
            newsTitle = view.findViewById(R.id.news_title);
            newsContext = view.findViewById(R.id.news_context);
            newsImage = view.findViewById(R.id.news_image);
            newsName = view.findViewById(R.id.news_name);

        }
    }

    public NewsRecyclerViewAdapter(List<News> newsList) {
        mNewsList = newsList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        News news = mNewsList.get(position);
        holder.newsDate.setText(news.getDate());
        holder.newsTitle.setText(news.getTitle());
        holder.newsContext.setText(news.getContext());
        holder.newsImage.setImageResource(news.getImage());
        holder.newsName.setText(news.getName());
    }

    @Override
    public int getItemCount() {
        return mNewsList.size();
    }
}


